<template>
  <div class="d-flex justify-content-center w-100 align-items-center" style="height: 24px; background-color: #4a5064; color: #ffffff; font-size: 10px; letter-spacing: 0.2em; cursor:pointer;" @click="menuSwitchClick">
    |||
  </div>
  <el-menu
    class="border-0"
    background-color="#333744"
    text-color="#ffffff"
    active-text-color="#409Eff"
    unique-opened
    :collapse="switchOvermenu"
    :collapse-transition="false"
    :default-active="activePathVal"
    router
  >
    <el-submenu :index="`/${item.path}`" v-for="item in menuList" :key="item.id">
      <template #title>
        <i :class="item.id ? iconObj[item.id] : ''"></i>
        <span class="ms-3">{{item.authName}}</span>
      </template>
      <template v-if="item.children">
        <el-menu-item
        :index="`/${menuItem.path}`"
        v-for="menuItem in item.children"
        :key="menuItem.id"
        @click="saveNavState('/' + menuItem.path)"
        >
          <i class="el-icon-menu"></i>
          {{menuItem.authName}}
        </el-menu-item>
      </template>
    </el-submenu>
  </el-menu>
</template>

<script lang="ts">
import { defineComponent, ref, computed, onMounted } from 'vue'
import { useStore } from '@/store'
import { menuData, menuListProps } from '../store/storeType'
import '../assets/fonts/iconfont'
import '../assets/fonts/iconfont.css'
interface objType {
  [key: string]: string
}

export default defineComponent({
  name: 'SubMenu',
  emits: ['control-width'],
  setup (props, { emit }) {
    const store = useStore()
    const switchOvermenu = ref(false)
    const activePathVal = ref('')
    const iconObj: objType = {
          '125':'iconfont icon-user',
          '103':'iconfont icon-tijikongjian',
          '101':'iconfont icon-shangpin',
          '102':'iconfont icon-danju',
          '145':'iconfont icon-baobiao'
        }
    const menuSwitchClick = () => {
      switchOvermenu.value = !switchOvermenu.value
      emit('control-width')
    }
    onMounted(() => {
      activePathVal.value = window.sessionStorage.getItem('activePath') as string
    })
    const menuList = computed<menuListProps<menuData>[]>(() => store.state.menuList)
    const saveNavState = (activePath: string) => {
      window.sessionStorage.setItem('activePath', activePath)
      activePathVal.value = activePath
    }
    return { switchOvermenu, menuSwitchClick, menuList, iconObj, saveNavState, activePathVal }
  }
})
</script>
